<?php
session_start();

include ('sqlconnection.php');
$db = new Database();
$db->connect();
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Property1</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="css/stylesheet.css" rel="stylesheet" type="text/css"/>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0rc2/jquery.mobile-1.0rc2.min.css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/mobile/1.0rc2/jquery.mobile-1.0rc2.min.js"></script>
<!-- This reference to phonegap.js will allow for code hints as long as the current site has been configured as a mobile application. 
	 To configure the site as a mobile application, go to Site -> Mobile Applications -> Configure Application Framework... -->
<script src="js/phonegap.js" type="text/javascript"></script>
</head>

<body>
<div data-role="page" id="page2" data-theme="c">
    <div data-role="header" data-theme="a"> <a data-rel="back" data-icon="back" data-icon="back">Back</a>
     <a href="logout.php" data-role="button" data-icon="delete" rel="external">Logout</a>
        <h1>Property</h1>
    </div>
    <div data-role="content">
        <?php
	$pid = $_GET["pid"];
	$percentage = $_GET['percent'];
	$query = "select * from property where pro_id =".$_GET["pid"] ;
	$result = mysql_query($query);
	$rows = mysql_fetch_array($result);
	$state = $rows['state'];
	$suburb = $rows['suburb'];
	$address = $rows['address'];
	$rating = 0;
	$ratingQuery = "select rating from reviews where pro_id = $pid";
	$result = mysql_query($ratingQuery);
	$rowCountForRating = mysql_num_rows($result);
	while($row = mysql_fetch_array($result)){
		$rating += $row['rating'];
	}
	
	if($rating != 0){

		$rating = floor($rating / $rowCountForRating);
	}
	
	
	$lat = $rows["lat"];
	$lng = $rows["lng"];
	$query6 = "select image_url from property_images where p_id =".$_GET["pid"] ;
	$result6 = mysql_query($query6);
	$rows6 = mysql_fetch_array($result6);
	?>
        <a href="propertyphoto.php?pid=<?php print $pid ?>" rel="external"> <img src="<?php echo $rows6["image_url"] ?>" height="200" width="100%" /> </a>
        <hr/>
        <ul data-role="listview">
            <li>
                <table width="100%">
                    <tr>
                        <td><?php print "<strong> $address </strong><br/>";
				print "<span style='font-size:10pt' > $suburb </span><br/>";
				
				print "<span style='font-size:10pt' > Compatibility: $percentage%</span><div name='cbar' class='cbar' style='width:".$percentage."px;'></div>";
				
				print "</td><td>";

            	print "<a href='propertymap.php?lat=$lat&lng=$lng&address=$address' rel='external'><img src='images/map_icon.png' width='30px' height='30px'/></a>";
          		print "</td></tr><tr><td>";
				

                print "<img src='./images/beds.png'/>";
				print "<span style='font-size:8pt' > ". $rows["bedroom"] . "</span>&nbsp;&nbsp";
               	
               	print "<img src='./images/baths.png'/>";
				print "<span style='font-size:8pt' > ". $rows["toilet"] . "</span>&nbsp;&nbsp";
               
                print "<img src='./images/garage.png'/>";
					print "<span style='font-size:8pt' > ". $rows["garage"] . "</span>";
				
				print "</td></tr><tr><td>";
				
				print "<span style='font-size:8pt' >Tenants:</span> ";
                $query3 = "select * from resident where pro_id = $pid and moveout_date IS NULL" ;
				$result3 = mysql_query($query3);
				
				while($rows3 = mysql_fetch_array($result3)){
					$residentName = $rows3['username'];
					
					$queryForProPic = "select profile_pic as p from usertable where username = '$residentName'";
					$resultProPic = mysql_query($queryForProPic);
					$profilePic = mysql_fetch_array($resultProPic);
					
					$url = $profilePic['p'];
					print "<a href='user_profile.php?username=$residentName' rel='external'><img src='$url' width='20' height='24' /></a>&nbsp;";
					
				}
				
				print "</td></tr>";
				
				
			 ?>
                </table>
            </li>
        </ul>
        <br>
        <br>
       
                <?php
			//$index = 0;
            /*while($rows7 = mysql_fetch_array($result6)){
				if($index < 4){
				$image_url = $rows7['image_url'];
            	echo "<a href='propertyphoto.php?pid=$pid' rel='external'><img src='$image_url' height='60' width='80' /></a>";
				}
				$index++;
			}*/
			?>

        <br>
        <br>
        <ul data-role="listview">
            <li data-role="header" data-theme='a' role="heading" >
            <table style='width:100%'><tr>
            <td width="20%">Reviews</td>
            <td>
            <?php
				for($i=1; $i<= 5; $i++){
					if($i <= $rating){
						print "<img src='images/star1.gif' width='15' height='15' />";
					}else {
						print  "<img src='images/star.gif' width='15' height='15' />";
					}
				
				}
			
			?>
            </td>
             <td style='text-align:center'>
            	<a href='#post232' data-role='button' data-theme='c'>Post</a>
            </td>
            </tr>
           
            
            </table>
            
            </li>
            <?php
         $query5 = "select * from reviews where pro_id =".$_GET["pid"];
		 $result5 = mysql_query($query5);
		 while($rows5 = mysql_fetch_array($result5)){
			$reviews = $rows5['reviews'];
			$reviewer = $rows5['username'];
			$userRatings = $rows5['rating'];
			
			echo "<li class=\"ui-li ui-body-c ui-li-static\">";
			echo "<p style='white-space:normal'> $reviews </p><br>";
			echo "<p>Rated:" ;
			for($i=1; $i<= 5; $i++){
				if($i <= $userRatings){
					print "<img src='images/star1.gif' width='15' height='15' />";
				}else {
					print  "<img src='images/star.gif' width='15' height='15' />";
				}
			}	
			
			echo "<p>"	;
			echo "<p> $reviewer </p>";
			echo "</li>";
		 }
		 ?>
        </ul>
    </div>
</div>

<!-- Start of first page -->
<div data-role="page" id="post232" data-theme="c">

	<div data-role="header" data-theme="a">
		<h1>Post your Reviews</h1>
	</div><!-- /header -->

	<div data-role="content">
    
	</div><!-- /content -->

	<div data-role="footer">
	</div><!-- /footer -->
</div><!-- /page -->
</body>
</html>